<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户uid" prop="userUid">
      <el-input v-model="dataForm.userUid" placeholder="用户uid"></el-input>
    </el-form-item>
    <el-form-item label="权益uid" prop="equityUid">
      <el-input v-model="dataForm.equityUid" placeholder="权益uid"></el-input>
    </el-form-item>
    <el-form-item label="权益类型(1:VIP特权, 2:签到卡, 3:兑换的资源)" prop="equityType">
      <el-input v-model="dataForm.equityType" placeholder="权益类型(1:VIP特权, 2:签到卡, 3:兑换的资源)"></el-input>
    </el-form-item>
    <el-form-item label="是否永久生效（0：否，1：是）" prop="isPermanent">
      <el-input v-model="dataForm.isPermanent" placeholder="是否永久生效（0：否，1：是）"></el-input>
    </el-form-item>
    <el-form-item label="使用状态（0：未使用，1：已使用，2：已过期）" prop="useStatus">
      <el-input v-model="dataForm.useStatus" placeholder="使用状态（0：未使用，1：已使用，2：已过期）"></el-input>
    </el-form-item>
    <el-form-item label="权益生效时间" prop="startTime">
      <el-input v-model="dataForm.startTime" placeholder="权益生效时间"></el-input>
    </el-form-item>
    <el-form-item label="权益截止时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="权益截止时间"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          userUid: '',
          equityUid: '',
          equityType: '',
          isPermanent: '',
          useStatus: '',
          startTime: '',
          endTime: '',
          status: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          userUid: [
            { required: true, message: '用户uid不能为空', trigger: 'blur' }
          ],
          equityUid: [
            { required: true, message: '权益uid不能为空', trigger: 'blur' }
          ],
          equityType: [
            { required: true, message: '权益类型(1:VIP特权, 2:签到卡, 3:兑换的资源)不能为空', trigger: 'blur' }
          ],
          isPermanent: [
            { required: true, message: '是否永久生效（0：否，1：是）不能为空', trigger: 'blur' }
          ],
          useStatus: [
            { required: true, message: '使用状态（0：未使用，1：已使用，2：已过期）不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '权益生效时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '权益截止时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/user/userequityrecord/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userUid = data.userEquityRecord.userUid
                this.dataForm.equityUid = data.userEquityRecord.equityUid
                this.dataForm.equityType = data.userEquityRecord.equityType
                this.dataForm.isPermanent = data.userEquityRecord.isPermanent
                this.dataForm.useStatus = data.userEquityRecord.useStatus
                this.dataForm.startTime = data.userEquityRecord.startTime
                this.dataForm.endTime = data.userEquityRecord.endTime
                this.dataForm.status = data.userEquityRecord.status
                this.dataForm.createTime = data.userEquityRecord.createTime
                this.dataForm.updateTime = data.userEquityRecord.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/userequityrecord/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'userUid': this.dataForm.userUid,
                'equityUid': this.dataForm.equityUid,
                'equityType': this.dataForm.equityType,
                'isPermanent': this.dataForm.isPermanent,
                'useStatus': this.dataForm.useStatus,
                'startTime': this.dataForm.startTime,
                'endTime': this.dataForm.endTime,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
